﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;

namespace UnitTests
{
  public class LoggableObject
  {
    private readonly List<LogMessage> _logMessages = new List<LogMessage>();

    [MethodImpl(MethodImplOptions.NoInlining)]
    protected void AutoLog(params string[] message)
    {
      var item = new StackFrame(1).GetMethod().Name;
      item += "(" + string.Join(", ", message) + ")";
      item = "{0}." + item;
      AppendLog(new LogMessage(item, this));
    }

    protected void AppendLog(LogMessage item)
    {
      _logMessages.Add(item);
      OnLogMessage(new LogMessageEventArgs(item));
    }

    public event EventHandler<LogMessageEventArgs> LogMessage;
    public void OnLogMessage(LogMessageEventArgs e)
    {
      var handler = LogMessage;
      if (handler != null) handler(this, e);
    }

    public string Log { get { return string.Join("; ", _logMessages); } }
    public ReadOnlyCollection<string> LogMessages
    {
      get
      { 
        return new ReadOnlyCollection<string>(
          _logMessages.Select(l => l.ToString()).ToList()); 
      }
    }
  }
}